package com.cms.cmsbackend.controller;

import com.cms.cmsbackend.mapper.CategoryMapper;
import com.cms.cmsbackend.pojo.Article;
import com.cms.cmsbackend.pojo.Category;
import com.cms.cmsbackend.service.ArticleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "信息管理")
@RequestMapping("/Article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    private CategoryMapper categoryMapper;


    @PostMapping("/add")
    @ApiOperation("信息新增")
    public String add(Article article){
        try {
            articleService.save(article);
            return "数据新增成功";
        }catch (RuntimeException e){
            return "数据操作失败  "+ e.getMessage();
        }
    }

    @GetMapping("/selectByPage")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true),
            @ApiImplicitParam(name = "author",value = "条件中的作者",required = false),
            @ApiImplicitParam(name = "title",value = "条件中的题目",required = false)
    })
    public PageInfo selectByPage(int page, int size,String author,String title){
        PageInfo pageInfo=articleService.findByPage(page,size,author,title);
        return pageInfo;
    }

    @GetMapping("/delete")
    @ApiOperation("信息删除")
    @ApiImplicitParam(name = "id",value = "删除信息id",required = true)
    public String del(int id){
        articleService.remove(id);
        return "删除成功";
    }

    @GetMapping("/batchdelete")
    @ApiOperation("批量删除")
    @ApiImplicitParam(name = "ids",value = "批量删除id,写法如下：1,2,3,4",required = true)
    public String batchdel(int[] ids){
        articleService.batchrem(ids);
        return "批量删除成功";
    }

    @GetMapping("/select")
    @ApiOperation("信息查询")
    @ApiImplicitParam(name="id",value = "查询信息id",required = true)
    public Article select(int id){

            Article article = articleService.choose(id);
            return article;


    }
    @PostMapping("/updata")
    @ApiOperation("信息更新")
    public String updata(Article article){
        articleService.updata(article);
        return "更新成功";
    }

}
